<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    protected $namespace = 'App\Http\Controllers';

    public function boot()
    {
        Route::pattern('id', '[0-9]+');

        parent::boot();
    }

    public function map()
    {
        $this->mapDefaultRoutes();

        $this->mapWebRoutes();

        $this->mapApiRoutes();
    }

    private function mapDefaultRoutes()
    {
        Route::namespace($this->namespace)->group(base_path('routes/default.php'));
    }

    private function mapWebRoutes()
    {
        Route::prefix('web')
            ->middleware(['api', 'web'])
            ->namespace($this->namespace . '\Web')
            ->group(function () {
                foreach (glob(base_path('routes') . '/web/*.php') as $file) {
                    require $file;
                }
            });
    }

    private function mapApiRoutes()
    {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace . '\Api')
            ->group(function () {
                foreach (glob(base_path('routes') . '/api/*.php') as $file) {
                    require $file;
                }
            });
    }
}
